package com.shengzidong.keepacounts.avtivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.shengzidong.keepacounts.R;
import com.shengzidong.keepacounts.adapter.CategoryListAdapter;
import com.shengzidong.keepacounts.entity.Category;
import com.shengzidong.keepacounts.entity.Receipt;
import com.shengzidong.keepacounts.utils.MyCategory;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReceiptInfoActivity extends AppCompatActivity implements View.OnClickListener {
    TextView tvDate, tvCategory, tvLable, tvLocation, tvMessage;
    EditText etMoney;
    Button btn_delete, btn_ok;
    ListView lvCategory;
    CategoryListAdapter adapter;
    private int height;
    Receipt receipt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receipt_info);
        initViews();

        Intent intent = getIntent();
        receipt = (Receipt) intent.getSerializableExtra("receipt");
        Log.d("123", receipt.toString());
        setDatas();

        MyCategory myCategory = new MyCategory(this);
        ArrayList<Category> categories = myCategory.getCategoryList();
        adapter = new CategoryListAdapter(this, categories);
        lvCategory.setAdapter(adapter);

        Display display = getWindowManager().getDefaultDisplay();
        height = display.getHeight();
        lvCategory.setTranslationY(height);

    }

    private void setDatas() {
        tvDate.setText(receipt.getDate());
        etMoney.setHint(String.valueOf(receipt.getMoney()));
        tvCategory.setText(receipt.getCategory());
        tvLable.setText(receipt.getLabel());
        tvLocation.setText(receipt.getLocation());
        tvMessage.setText(receipt.getMessage());

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvDate:
                final Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d("123", "您选择的日期" + year + "-" + (month + 1) + "-" + day);
                        String mMonth = String.valueOf(month + 1);
                        String mday = String.valueOf(day);

                        if (mMonth.length() == 1) {
                            mMonth = "0" + mMonth;
                        }
                        if (mday.length() == 1) {
                            mday = "0" + mday;
                        }

                        tvDate.setText(year + "-" + mMonth + "-" + mday);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH)).show();

                break;
            case R.id.etMoney:
                etMoney.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean b) {
                        if (!b) {
                            String money = etMoney.getText().toString();
                            Pattern pattern = Pattern.compile("-?[0-9]*");
                            Matcher matcher = pattern.matcher(money);
                            if (!matcher.matches()) {
                                Toast.makeText(ReceiptInfoActivity.this, "输入的数字不对", Toast.LENGTH_SHORT).show();
                                etMoney.setText("");
                            }
                        }
                    }
                });
                break;
            case R.id.tvCategory:
                TranslateAnimation animation = new TranslateAnimation(0, 0, height, 0);
                lvCategory.startAnimation(animation);

                break;
            case R.id.tvLable:


                break;
            case R.id.tvLocation:

                break;
            case R.id.tvMessage:

                break;
            case R.id.btn_delete:
                Log.d("ReceiptInfoActivity","记录删除");

                Intent intent=new Intent();
                intent.putExtra("receipt",receipt);
                setResult(RESULT_OK,intent);
                this.finish();

                break;
            case R.id.btn_Ok:

                break;
        }
    }

    private void initViews() {
        tvDate = (TextView) findViewById(R.id.tvDate);
        etMoney = (EditText) findViewById(R.id.etMoney);
        tvCategory = (TextView) findViewById(R.id.tvCategory);
        tvLable = (TextView) findViewById(R.id.tvLable);
        tvLocation = (TextView) findViewById(R.id.tvLocation);
        tvMessage = (TextView) findViewById(R.id.tvMessage);
        lvCategory = (ListView) findViewById(R.id.lvCategory);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_ok = (Button) findViewById(R.id.btn_Ok);


        tvDate.setOnClickListener(this);
        etMoney.setOnClickListener(this);
        tvCategory.setOnClickListener(this);
        tvLable.setOnClickListener(this);
        tvLocation.setOnClickListener(this);
        tvMessage.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_ok.setOnClickListener(this);
    }


}
